#ifndef STRING_CAST_H
#define STRING_CAST_H

#include <string>

namespace base::utils
{
/**
 * 字符串类型转数字类型
 *
 * @author: haitao.lin
 */
class StringCast
{
public:
    /**
     * Returns the string converted to an int using base base,
     * which is 10 by default and must be between 2 and 36, or 0. Returns 0 if the conversion fails.
     * If a conversion error occurs, *ok is set to false; otherwise *ok is set to true.
     * If base is 0, the C language convention is used: If the string begins with "0x",
     * base 16 is used; if the string begins with "0", base 8 is used; otherwise, base 10 is used.
     */
    static int toInt(const std::string &str, bool *ok = nullptr, int base = 10);

    /**
     * Returns the string converted to an long long int using base base
     */
    static long long toLongLong(const std::string &str, bool *ok = nullptr, int base = 10);

    /**
     * Returns the string converted to an unsigned int using base base,
     * which is 10 by default and must be between 2 and 36, or 0. Returns 0 if the conversion fails.
     * If a conversion error occurs, *ok is set to false; otherwise *ok is set to true.
     * If base is 0, the C language convention is used: If the string begins with "0x",
     * base 16 is used; if the string begins with "0", base 8 is used; otherwise, base 10 is used.
     */
    static unsigned int toUInt(const std::string &str, bool *ok = nullptr, int base = 10);

    /**
     * Returns the string converted to an unsigned long long int using base base
     */
    static unsigned long long toULongLong(const std::string &str, bool *ok = nullptr, int base = 10);

    /**
     * Returns the string converted to a double value.
     * Returns 0.0 if the conversion fails.
     * If a conversion error occurs, *ok is set to false; otherwise *ok is set to true.
     *
     * Warning: The string content may only contain valid numerical characters which includes the plus/minus sign,
     * the characters g and e used in scientific notation, and the decimal point.
     * Including the unit or additional characters leads to a conversion error.
     * Example:
     * bool is_ok = false;
     * toDouble("1234.56e-02", &is_ok);//12.3456, true
     */
    static double toDouble(const std::string &str, bool *ok = nullptr);

    /**
     * Returns the string converted to a double value.
     * Returns 0.0 if the conversion fails.
     * If a conversion error occurs, *ok is set to false; otherwise *ok is set to true.
     */
    static long double toLongDouble(const std::string &str, bool *ok = nullptr);

    /**
     * Returns the string converted to a float value.
     * If a conversion error occurs, *ok is set to false;
     * otherwise *ok is set to true. Returns 0.0 if the conversion fails.
     * The string conversion will always happen in the 'C' locale.
     */
    static float toFloat(const std::string &str, bool *ok = nullptr);

private:
    StringCast();
};

}

#endif //STRING_CAST_H
